<template>
  <div class="productWrapper">
    <div class="handle-box">
      <el-input v-model="paramObj.name" placeholder="商品名称" class="handle-input mr10"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      <el-button type="primary" icon="edit" @click="create()" class="create-btn">新建商品</el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
      <el-table-column prop="originPrice" label="原价" align="center"></el-table-column>
      <el-table-column prop="presentPrice" label="现价" align="center"></el-table-column>
      <el-table-column prop="proportion" label="团长佣金占比%" align="center"></el-table-column>
      <el-table-column prop="downline" label="状态" align="center" :formatter="onlineFormat"></el-table-column>
      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="scope">
          <el-button size="small" @click="handleView(scope.$index, scope.row)">查看</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button size="small" @click="handleDownline(scope.$index, scope.row)">
            {{scope.row.downline==0 ? '下线' : '上线'}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount" :page-size="paramObj.limit" @current-change="handleCurrentChange">
    </el-pagination>
    
  </div>

</template>

<script>
import api from "@/api/product";

const SUCCESS = 0;
const LIMIT = 10;
export default {
  data() {
    return {
      tableData: [],
      totalCount: 0,
      optId: "",
      paramObj: {
        name: null,
        currentPage: 1,
        limit: LIMIT
      }
    };
  },
  created() {
    this._getData();
  },
  methods: {
    onlineFormat(row, column) {
      let downline = row[column.property];
      if (downline == 0) {
        return "在线";
      } else {
        return "下线";
      }
    },
    handleView(index, row) {
      this.$router.push({ path: "/product/detail", query: { id: row.id } });
    },
    handleCurrentChange(val) {
      this.paramObj.currentPage = val;
      this._getData();
    },
    handleDelete(index, row) {
      this.optId = row.id;

      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureDelete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    _sureDelete() {
      api.delete(this.optId).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        }
      });
    },
    _getData() {
      api.getList(this.paramObj).then(response => {
        if (response.code == SUCCESS) {
          this.tableData = response.data.productList;
          this.totalCount = response.data.totalCount;
        }
      });
    },
    handleEdit(index, row) {
      this.$router.push({ path: "/product/edit", query: { id: row.id } });
    },
    create() {
      this.$router.push({ path: "/product/edit" });
    },
    handleDownline(index, row) {
      let downline;
      if (row.downline == 0) {
        downline = 1;
      } else {
        downline = 0;
      }
      api.setDownline(row.id, downline).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "设置成功!"
          });
        }
      });
    },
    search() {
      this.paramObj.currentPage = 1;
      this._getData();
    }
  }
};
</script>

<style lang="less">
.productWrapper {
  .handle-box {
    margin-bottom: 20px;
    .handle-select {
      width: 240px;
    }
    .handle-input {
      width: 200px;
      display: inline-block;
    }
    .create-btn {
      float: right;
      margin-right: 20px;
    }
  }
}
</style>